const {
  mysql
} = require('../qcloud');

const {
  mysqlQuery
} = require('../tools/utils');

module.exports = async ctx => {
  let query = ctx.request.query;
  if (ctx.request.query.from == 'index') {
    let list = await mysql('video')
      .select('*')
      .orderBy('counter', 'desc')
      .limit(6)
    ctx.state.data = {
      list
    }
  } else if (ctx.request.query.from == 'video') {
    let listHot = await mysql('video')
      .select('*')
      .orderBy('counter', 'desc')
      .limit(12)
    let listNew = await mysql('video')
      .select('*')
      .orderBy('create_time', 'desc')
      .limit(12)
    ctx.state.data = {
      listHot,
      listNew
    }
  } else if (query.type == 'hot') {
    let pageIndex = query.pageIndex;
    let size = 18;
    let hotList = await mysql('video')
      .select('*')
      .orderBy('counter', 'desc')
      .limit(size)
      .offset(Number(pageIndex) * size)
    ctx.state.data = {
      hotList
    }

  } else if (query.type == 'new') {
    let pageIndex = query.pageIndex;
    let size = 18;
    let hotList = await mysql('video')
      .select('*')
      .orderBy('create_time', 'desc')
      .limit(size)
      .offset(Number(pageIndex) * size)
    ctx.state.data = {
      hotList
    }
  } else if (query.type == 'tuijian') {
    let sql = `SELECT * from video ORDER BY RAND() LIMIT 6`
    let list = await mysqlQuery(sql)
    ctx.state.data = {
      list
    }
  }


  /* else {
    let list = await mysql('video')
      .select('*')
      .orderBy('counter', 'desc')
    // .limit(6)
    ctx.state.data = {
      list
    }
  } */
}
